package com.education.springbootdemo.service;

import com.education.springbootdemo.dao.UserDao;
import com.education.springbootdemo.mapper.UserMapper;
import com.education.springbootdemo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDao userDao;

    public Long insertUser(User user) {
        //1、先从数据库中查询是否有同名，
       Long ret = userDao.inserUser(user);

       if(ret > 0) {
           //2、 其他的业务逻辑
       }

        return ret;
    }

    public User selectById(Long id) {
        return userMapper.selectById(id);
    }

    public User selectByUsernameAndPassword(String username, String password) {
        return userMapper.selectByUsernameAndPassword(username,password);
    }

    public User reg(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        Long id = userMapper.regUser(user);
        if(id > 0 && user.getId() !=null) {
            return userMapper.selectById(user.getId());
        }
        return null;
    }

    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }
}
